//
//  MassageOperationVC.m
//  massage
//
//  Created by 李志权 on 2016/12/15.
//  Copyright © 2016年 李志权. All rights reserved.
//

#import "MassageOperationVC.h"
#import "CKCircleView.h"
@interface MassageOperationVC ()<UIPickerViewDelegate,UIPickerViewDataSource>
{
    NSArray *styleViewArray;
    NSArray *gearsViewArray;
}
/**渐变VIEW*/
@property (weak, nonatomic) IBOutlet UIView *gradientView;
/**进度条*/
@property (nonatomic,strong)CKCircleView *dialView;
@property (weak, nonatomic) IBOutlet UIView *timeView;
/**按摩样式*/
@property (weak, nonatomic) IBOutlet UIPickerView *styleView;
/**档位*/
@property (weak, nonatomic) IBOutlet UIPickerView *gearsView;
@end

@implementation MassageOperationVC
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self pickerView:self.styleView didSelectRow:0 inComponent:0];
    [self pickerView:self.gearsView didSelectRow:0 inComponent:0];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [Utils navigationBarReturn:self color:[UIColor clearColor] font:fontSizeTitleBold foregroundColor:[UIColor whiteColor]];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = YES;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"登录_left-arrow"] style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonItemClick)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    UIView *backgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Width, 364)];
    UIColor *colorOne = [UIColor colorWithRed:(49/255.0)  green:(255/255.0)  blue:(132/255.0)  alpha:1];
    UIColor *colorTwo = [UIColor colorWithRed:(2/255.0)  green:(200/255.0)  blue:(250/255.0)  alpha:1];
    
    CAGradientLayer *gradientLayer =  [CAGradientLayer layer];
    gradientLayer.frame = CGRectMake(0, 0, Width, 364);
    //设置颜色
    [gradientLayer setColors:[NSArray arrayWithObjects:(id)[colorOne CGColor],(id)[colorTwo CGColor], nil]];
    //每种颜色最亮的位置
    //    [gradientLayer setLocations:@[@.5,@.5]];
    //渐变的方向StartPoint－>EndPoint
    [gradientLayer setStartPoint:CGPointMake(0, 0)];
    [gradientLayer setEndPoint:CGPointMake(1, 1)];
    [backgroundView.layer addSublayer:gradientLayer];
    
    self.gradientView.backgroundColor = [UIColor colorWithPatternImage:[self getImageFromView:backgroundView]];
    
    self.dialView = [[CKCircleView alloc] initWithFrame:CGRectMake(3,9, 260, 245)];
    self.dialView.backColor = [UIColor clearColor];
    self.dialView.dialColor = [UIColor whiteColor];//点颜色
    self.dialView.arcRadius = 255;
    self.dialView.dialRadius = 15.0;//点大小*2
    self.dialView.arcThickness = 3.0;
    [self.timeView addSubview: self.dialView];
    
    styleViewArray = @[@"敲击",@"针灸",@"揉捏",@"按压",@"敲打"];
    gearsViewArray = @[@"1档",@"2档",@"3档",@"4档"];
   
    // Do any additional setup after loading the view from its nib.
}
-(UIImage *)getImageFromView:(UIView *)theView
{
    // 下面方法，第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果，需要传NO，否则传YES。第三个参数就是屏幕密度了，关键就是第三个参数。\
    
    CGSize size =theView.bounds.size;
    UIGraphicsBeginImageContextWithOptions(size, YES,  [UIScreen mainScreen].scale);
    [theView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image=UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}
- (void)leftBarButtonItemClick
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
//选取器视图的数据源以及委托
#pragma mark - UIPickerViewDataSource
//返回一共有多少组
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    if (pickerView == self.styleView) {
        PickerViewCell *pickerCell = (PickerViewCell *)view;
        if (!pickerCell) {
            pickerCell = [[PickerViewCell alloc] initWithFrame:CGRectMake(0, 0, 80, 20)];
        }
        pickerCell.title.font =fontSizeMinMin;
        pickerCell.line1.hidden = YES;
        pickerCell.line1.hidden = YES;
        pickerCell.title.textColor = GrayLight;
        pickerCell.title.text = styleViewArray[row];
        ((UIView *)[pickerView.subviews objectAtIndex:1]).backgroundColor = [UIColor clearColor];
        
        ((UIView *)[pickerView.subviews objectAtIndex:2]).backgroundColor = [UIColor clearColor];
        return pickerCell;
    }
    if (pickerView == self.gearsView) {
        PickerViewCell *pickerCell = (PickerViewCell *)view;
        if (!pickerCell) {
            pickerCell = [[PickerViewCell alloc] initWithFrame:CGRectMake(0, 0, 80, 20)];
        }
        pickerCell.title.font =fontSizeMinMin;
        pickerCell.line1.hidden = YES;
        pickerCell.line1.hidden = YES;
        pickerCell.title.textColor = GrayLight;
        pickerCell.title.text = gearsViewArray[row];
        ((UIView *)[pickerView.subviews objectAtIndex:1]).backgroundColor = [UIColor clearColor];
        
        ((UIView *)[pickerView.subviews objectAtIndex:2]).backgroundColor = [UIColor clearColor];
        return pickerCell;
    }
    return nil;
}
//返回每一组有多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (pickerView == self.styleView) {
        return styleViewArray.count;
    }
    if (pickerView == self.gearsView) {
        return gearsViewArray.count;
    }
    return 0;
}

#pragma mark - UIPickerViewDelegate
//返回每一组的宽度
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    return 80;
}

//返回每一组的行高
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 20;
}

//返回每一组每一行的标题
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    
    if (pickerView == self.styleView) {
        return styleViewArray[row];
    }
    if (pickerView == self.gearsView) {
        return gearsViewArray[row];
    }
    return @"";
}

//选择了某一组的某一行
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
   PickerViewCell *pickerCell =(PickerViewCell *)[pickerView viewForRow:row forComponent:component];
    pickerCell.line1.hidden = NO;
    pickerCell.line2.hidden = NO;
    pickerCell.title.textColor = GreenColor;
    pickerCell.title.font = fontSizeBigBig;
}
@end

@implementation PickerViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.line1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 1, self.highly)];
        self.line1.hidden = YES;
        self.line1.backgroundColor = GreenColor;
        [self addSubview:self.line1];
        
        self.title = [[UILabel alloc]initWithFrame:CGRectMake(self.line1.XWidth, 0, self.width-2, self.highly)];
        self.title.textAlignment = NSTextAlignmentCenter;
        self.title.textColor = GreenColor;
        self.title.font = fontSizeBigBig;
        [self addSubview:self.title];
        
        self.line2 = [[UIView alloc]initWithFrame:CGRectMake(self.title.XWidth, 0, 1, self.highly)];
        self.line2.hidden = YES;
        self.line2.backgroundColor = GreenColor;
        [self addSubview:self.line2];
        
    }
    return self;
}

@end
